package com.wzr.furerp.user.controller;

import com.google.gson.Gson;
import com.wzr.furerp.excel.MyXlsxView;
import com.wzr.furerp.user.dao.entity.User;
import com.wzr.furerp.user.dao.service.UserService;
import com.wzr.furerp.user.exception.UserException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/furerp")
public class LoginController {
    private static final String MODULE_URL = "furerp/";


    private static final String LOGIN_USERNAME = "loginUsername";

    private static Log log = LogFactory.getLog(LoginController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value="/login", method= RequestMethod.GET)
    public void Login(){
        return ;
    }

    @RequestMapping(value="/loginSubmit", method=RequestMethod.POST)
    @ResponseBody
    public String Login(@RequestParam String username, @RequestParam String password,
                        HttpSession session) {
        User user = new User();
        if(null != username && !username.trim().isEmpty() && null != password){
            user.setUsername(username);
            user.setPassword(password);
            if(!userService.UserExistByName(user.getUsername())){
                throw new UserException(UserException.EXCEP_USERNAME);
            }
            else if(!userService.UserPasswordValid(user)){
                throw new UserException(UserException.EXCEP_PASSWORD);
            }
            session.setAttribute(LOGIN_USERNAME, user.getUsername());
        }
        else {
            log.error("没有取到用户名");
            throw new UserException("没有取到用户名");
        }

        Gson gson = new Gson();
        Map<String, String> reUrl = new HashMap<String,String>();
        reUrl.put("hrefUrl",MODULE_URL + "index");
        return gson.toJson(reUrl);
    }

    @RequestMapping ("/index")
    public void furerpIndex(){
        return ;
    }

    @RequestMapping(value="/logout", method=RequestMethod.GET)
    public String Logout(HttpSession session){
        if(null != session.getAttribute(LOGIN_USERNAME)){
            session.invalidate();
        }
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "login";
    }

    /**
     * 根据JSON格式的字符串生成xlsx文件
     */
    @RequestMapping(value = "index/saveAsXlsx", method = RequestMethod.POST)
    public ModelAndView saveAsXlsx(ModelMap model){

        MyXlsxView ve = new MyXlsxView();
        return new ModelAndView(ve, model);
    }

    // 局部异常处理, 发生异常时返回登录界面
    @ExceptionHandler(UserException.class)
    public void handlerException(Exception excep, HttpServletResponse rsp) throws IOException {
        Map<String,String> map = new HashMap<>();
        map.put("excep", excep.getMessage());
        log.error(excep);
        PrintWriter out = rsp.getWriter();
        Gson gson = new Gson();
        out.print(gson.toJson(map));
    }
}
